package zd.cornermemory.utils;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import zd.cornermemory.db.Cj;

import static java.lang.Math.abs;

public class Graph {
	private static int getDivision(int dv) {
		int[] ds = {100, 200, 500, 1000, 2000, 5000, 10000, 20000, 30000, 60000, 90000, 120000, 300000, 600000, 1200000, 1800000, 3600000};
		if(dv < ds[0]) return 100;
		for(int i=1; i<17; i++)
			if(dv < ds[i]) return ds[i];
		return (dv/1000+1)*1000;
	}
	
	private static String distime(int i) {
		boolean m = i < 0;
		i = abs(i) + 5;
		int ms = (i % 1000) / 100;
		int s = i / 1000;
		int mi = 0, h = 0;
		mi = s / 60;
		s %= 60;
		h = mi / 60;
		mi %= 60;
		return (m?"-":"")+(h>0?h+":":"")+(h>0?(mi<10?"0"+mi+":":mi+":"):(mi>0?mi+":":""))+(((h>0 || mi>0) && s<10)?"0":"")+s+"."+ms;
	}
	
	public static void drawHist(int width, int height1,Paint p, Canvas c, List<Cj> cjList) {
		int[] bins = new int[28];
		int start;
		int end;
		if(cjList==null||cjList.size()==0) {
			start = 13000;
			end = 27000;
		} else {
			List<Cj> minMax = new ArrayList<>();
			for (Cj cj : cjList) {
				if (cj.getStatus() != 2) {
					minMax.add(cj);
				}
			}
			int max = Collections.max(minMax).getSingTime().intValue();
			int min = Collections.min(minMax).getSingTime().intValue();
			int divi = getDivision((max - min) / 14);
			int mean = (min + max) >>> 1;
			mean = ((mean + divi / 2) / divi) * divi;
			start = mean - divi * 7;
			end = mean + divi * 7;
		}
		for (int i = 0; i < bins.length; i++)
			bins[i] = 0;
		for (int i = 0; i < cjList.size(); i++) {
			if (cjList.get(i).getStatus() != 2) {
				int time = cjList.get(i).getSingTime().intValue();
				if(time >= start && time < end) {
					int bin = (int) (bins.length * (time - start) / (end - start));
					bins[bin]++;
				}
			}
		}
		int wBase = 30*width/288;
//		c.drawLine(wBase, 0, wBase, (int)(width*1.2), p);
		c.drawLine(wBase, 0, wBase, height1, p);
		float wBar = (float) (height1/ (bins.length+1));
		for (int i = 0; i < bins.length+1; i++) {
			float y = (float) ((i + 0.5) * wBar);
			c.drawLine(wBase - 4, y, wBase + 4, y, p);
		}
		float binInterval = (float)(end - start) / bins.length;
		p.setTextSize(wBase*2/6);
		p.setTextAlign(Align.RIGHT);
		FontMetrics fm = p.getFontMetrics();
		float fontHeight = fm.bottom - fm.top;
		for (int i = 0; i < bins.length+1; i++) {
			int value = (int)(start + i * binInterval);
			float y = (float) ((i + 0.5) * wBar + fontHeight / 2 - fm.bottom);
			c.drawText(distime(value), wBase-5, y, p);
		}
		int maxValue = 0;
		for (int i = 0; i < bins.length; i++) {
			if (bins[i] > maxValue)
				maxValue = bins[i];
		}
		if (maxValue > 0) {
			p.setTextAlign(Align.CENTER);
			Paint.FontMetrics fontMetrics = p.getFontMetrics();
			float top = fontMetrics.top;//为基线到字体上边框的距离,即上图中的top
			float bottom = fontMetrics.bottom;//为基线到字体下边框的距离,即上图中的bottom
			for (int i = 0; i < bins.length; i++) {
				float y1 = (float) ((i + 0.5) * wBar);
				float y2 = (float) ((i + 1.5) * wBar);
				int height = (int) (bins[i] * (width - wBase - 4) / maxValue);
				p.setStyle(Paint.Style.FILL);
				p.setColor(Color.WHITE);
				c.drawRect(wBase + height, y1, wBase, y2, p);
				p.setColor(Color.BLACK);
				if ( 0 < bins[i] ) {
					String the_time = "" + bins[i];
					if ( p.measureText(the_time) < height ) {
						c.drawText(the_time, wBase + height / 2, y1 + abs(y2 - y1) / 2 - top / 2 - bottom / 2, p);
					}
					else
					{
						c.drawText(the_time, wBase + height + p.measureText(the_time) / 2, y1 + abs(y2 - y1) / 2 - top / 2 - bottom / 2, p);
					}
				}
				p.setStyle(Paint.Style.STROKE);
				p.setColor(Color.BLACK);
				c.drawRect(wBase + height, y1, wBase, y2, p);
			}
		}
	}

	public static void drawGraph(int width, int height1,Paint p, Canvas c, List<Cj> cjList) {
		int up, down, mean;
		int blk, divi;
		if(cjList==null||cjList.size()==0) {
			up = 20000;
			down = 12000;
			mean = 16000;
			blk = 8;
			divi = 1000;
		} else {
			List<Cj> temp = new ArrayList<>();
			for (Cj cj : cjList) {
				if (cj.getStatus() != 2) {
					temp.add(cj);
				}
			}
			int max = Collections.max(temp).getSingTime().intValue();
			int min = Collections.min(temp).getSingTime().intValue();
			divi = getDivision((max-min)/20);
			mean = (min + max) >>> 1;
			mean = ((mean + divi / 2) / divi) * divi;
			up = down = mean;
			while (up < max) {
				up += divi;
			}
			while (down > min) {
				down -= divi;
			}

			int count = 0;
			for (Cj cj : temp) {
				int ti = cj.getSingTime().intValue();
				count += ti;
			}

			mean = count/temp.size();
			blk = (up - down) / divi;
		}
		int wBase = 30 * width / 288;
		p.setTextSize(wBase *2 / 7);
		p.setTextAlign(Align.RIGHT);
		FontMetrics fm = p.getFontMetrics();
		float fontHeight = fm.bottom - fm.top;

		float wBar = (float) ((height1 - wBase / 4.5) / blk);
		p.setColor(0xff808080);
		p.setStrokeWidth(1);
		for (int i = 0; i < blk+1; i++) {
			if (i == 0 || i == blk) {
				p.setStrokeWidth(2);
			}else {
				p.setStrokeWidth(1);
			}
			float y = (float) (i * wBar + wBase/9.);
			c.drawLine(wBase, y, width, y, p);//分割线
		}
		p.setStrokeWidth(2);
		c.drawLine(wBase, (float)(wBase/9.), wBase, (float)(height1-wBase/9.), p);//左侧竖线
		c.drawLine(width, (float)(wBase/9.), width, (float)(height1-wBase/9.), p);//右侧竖线
		float y = (float) ((double)(up - mean) / divi * wBar + wBase/9.);
		p.setStrokeWidth(1);
		p.setColor(0xff000000);
		p.setPathEffect(null);
		for (int i = 0; i < blk+1; i++) {
			int value = (int)(up - i * divi);
			y = (float) (i * wBar + wBase/9. + fontHeight / 2 - fm.bottom);
			c.drawText(distime(value), wBase-10, y, p);//左侧文字
		}
		int count = cjList.size();

		p.setColor(Color.parseColor("#000000"));
		float rsp = (float) ((double)(width - 8 - wBase) / (count-1));

		count = 0;
		float lastx = -1, lasty = -1;
		for(int i=0; i<cjList.size(); i++) {
			if (cjList.get(i).getStatus() != 2) {
				int time = cjList.get(i).getSingTime().intValue();
				float x = (float) (wBase + 4.0 + (count) * rsp);
				y = (float) ((double)(up - time) / divi * wBar + wBase/9.);
				//c.drawRect(x-2, y-2, x+2, y-2, p);
//				c.drawCircle(x, y, 3, p);//圆点
				if(lastx!=-1){
					c.drawLine(lastx, lasty, x, y, p);//折线
				}
				lastx = x; lasty = y;
				count++;
			}else{
				count++;
			}
		}




		List<Cj> ao5List = split(cjList, 5);
		p.setColor(Color.parseColor("#0000FF"));
		p.setStrokeWidth(2);
		count = 0;
		float lastx_ao5 = -1, lasty_ao5 = -1;
		for(int i=0; i<ao5List.size(); i++) {
			if (ao5List.get(i).getStatus() != 2) {
				int time = ao5List.get(i).getSingTime().intValue();
				float x = (float) (wBase + 4.0 + (count) * rsp);
				y = (float) ((double)(up - time) / divi * wBar + wBase/9.);
				//c.drawRect(x-2, y-2, x+2, y-2, p);
//				c.drawCircle(x, y, 3, p);//圆点
				if(lastx_ao5!=-1) c.drawLine(lastx_ao5, lasty_ao5, x, y, p);//折线
				lastx_ao5 = x; lasty_ao5 = y;
				count++;
			}else{
				count++;
			}
		}

		List<Cj> ao12List = split(cjList, 12);
		p.setColor(Color.parseColor("#FF0000"));
		count = 0;
		float lastx_ao12 = -1, lasty_ao12 = -1;
		for(int i=0; i<ao12List.size(); i++) {
			if (ao12List.get(i).getStatus() != 2) {
				int time = ao12List.get(i).getSingTime().intValue();
				float x = (float) (wBase + 4.0 + (count) * rsp);
				y = (float) ((double)(up - time) / divi * wBar + wBase/9.);
				//c.drawRect(x-2, y-2, x+2, y-2, p);
//				c.drawCircle(x, y, 3, p);//圆点
				if(lastx_ao12!=-1) c.drawLine(lastx_ao12, lasty_ao12, x, y, p);//折线
				lastx_ao12 = x; lasty_ao12 = y;
				count++;
			}else {
				count++;
			}

		}

		p.setStrokeWidth(4);
		p.setColor(Color.parseColor("#FF0000"));
		float y_mean = (float) ((double)(up - mean) / divi * wBar + wBase/9.);
		p.setPathEffect(new DashPathEffect(new float[]{10, 10}, 0));
		c.drawLine(wBase, y_mean, width, y_mean, p);//平均线


	}


	public static List<Cj> split(List<Cj> list, int len) {
		List<Cj> cjList = new ArrayList<>();
		if(len==5){
			for(int i=0;i<4;i++) {
				cjList.add(new Cj(0L, 2));
			}
		} else if (len == 12) {
			for(int i=0;i<11;i++) {
				cjList.add(new Cj(0L, 2));
			}
		}
		if (list == null || list.size() == 0 || len < 1) {
			return cjList;
		}
		//传入集合长度
		int size = list.size();
		for (int i = 0; i < size; i++) {
			if (i > (size - len)) {
				break;
			}
			List<Cj> subList = list.subList(i, (i + len) > size ? size : i + len);
			long avg = getAvgInt(subList);
			if (avg == 0) {
				cjList.add(new Cj(avg, 2));
			}else{
				cjList.add(new Cj(avg, 0));
			}

		}

		return cjList;
	}

	public static long getAvgInt(List<Cj> cjList) {
		List<Cj> tempList = new ArrayList<>();
		long timeCount = 0;//
		int dnfCount = 0;
		Cj max = null;
		for(int i = 0;i<cjList.size();i++) {
			Cj cj = cjList.get(i);
			if (cj.getStatus() == 2) {
				dnfCount++;
				max = cj;
			}
			timeCount+=cj.getSingTime();
			tempList.add(cj);
		}

		if (cjList.size() == 3) {
			if (dnfCount > 0) {
				return 0;
			}else {
				double avg = Math.round(timeCount/cjList.size());
				return (long)avg;
			}
		}else {
			if (dnfCount == 0) {
				Cj min = Collections.min(tempList);
				max = Collections.max(tempList);
				tempList.remove(min);
				tempList.remove(max);
				long time = 0;
				for (Cj c : tempList) {
					time += c.getSingTime();
				}
				double avg = Math.round(time/tempList.size());
				return (int)avg;
			} else if (dnfCount == 1) {
				tempList.remove(max);
				Cj min = Collections.min(tempList);
				tempList.remove(min);
				long time = 0;
				for (Cj c : tempList) {
					time += c.getSingTime();
				}
				double avg = Math.round(time/tempList.size());
				return (long)avg;
			}else{
				return 0;
			}
		}


	}

}
